package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.item.to.CategoryView;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/inner/product")
public class SkuDetailRpcController {

    @Autowired
    BaseCategory1Service baseCategory1Service;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;

//    根据三级分类id，查询整个路径
//    c3Id  当前商品所属的三级分类id
    @GetMapping("/categoryview/{c3id}")
    public Result<CategoryView> getCategoryView(@PathVariable("c3id") Long c3Id){

        CategoryView categoryView = baseCategory1Service.getCategoryView(c3Id);

        return Result.ok(categoryView);
    }

//    商品的基本信息
    @GetMapping("/skuinfo/{skuId}")
//    使用SkuInfo会多返回一些数据
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){

        SkuInfo skuInfo = skuInfoService.getById(skuId);

        return Result.ok(skuInfo);
    }


//    查询价格
    @GetMapping("/price/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId){

        SkuInfo one = skuInfoService.lambdaQuery()
                .select(SkuInfo::getPrice)  //查询指定的列
                .eq(SkuInfo::getId, skuId)
                .one();

        return Result.ok(one.getPrice());
    }


//    查询图片
    @GetMapping("/images/{skuId}")
    public Result<List<SkuImage>> getImages(@PathVariable("skuId") Long skuId){
        List<SkuImage> skuImages = skuImageService.lambdaQuery()
                .eq(SkuImage::getId, skuId)
                .list();

        return Result.ok(skuImages);
    }

//    查询当前sku对应的spu定义的所有销售属性名和值，并标记出当前sku属于哪一种组合

    @GetMapping("/spusaleattr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId,
                                                    @PathVariable("skuId") Long skuId){

        List<SpuSaleAttr> spuSaleAttrList= spuSaleAttrService.getSpuSaleAttrAndMarkSku(spuId,skuId);
        return Result.ok(spuSaleAttrList);
    }


//      查询当前sku对应的spu当时录入的所有sku的每一种销售属性组合
    @GetMapping("/valueskujson/{spuId}")
    public Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId){

        String json = spuSaleAttrService.getValueSkuJson(spuId);
        return Result.ok(json);
    }
}
